package cn.tworice.client.service;

import cn.tworice.admin.service.InductsListenerService;
import cn.tworice.client.dao.PortDao;
import cn.tworice.client.dao.SacnDao;
import cn.tworice.common.entity.code.*;
import cn.tworice.common.vo.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SacnService implements InductsListenerService<Sacn> {
    @Autowired
    private SacnDao dao;

    @Autowired
    private PortDao portDao;

    @Autowired
    private PortService portService;

    @Autowired
    private InfoService infoService;

    Map<Integer, Integer> portList = null;

    public List<Sacn> getList(Integer page, Integer pageSize, Sacn entity) {
        return dao.queryList(page * pageSize, pageSize, entity);
    }

    public int getTotal(Sacn entity) {
        return dao.queryTotal(entity);
    }

    public void set(Sacn entity) {

        if (entity.getId() == null) {
            long id=(long)((Math.random()*9+1)*10000000);
            entity.setId(id);
            entity.setDate(System.currentTimeMillis());
            dao.insert(entity);
        } else {
            dao.update(entity);
        }

    }

    /**
     * 保存此次扫描记录
     * @param entity
     * @param infos
     */
    public void saveInfo(Sacn entity,List<Info> infos){
        long id=(long)((Math.random()*9+1)*10000000);
        entity.setId(id);
        entity.setDate(System.currentTimeMillis());
        dao.insert(entity);
        infos.forEach(item->{
            item.setSacn(id);
        });
        infoService.save(infos);
    }

    public void del(Integer[] ids) {
        dao.del(ids);
        infoService.delBySacn(ids);
    }

    @Override
    public int save(List<Sacn> list) {
        list.forEach(item -> {
            item.setId((long)((Math.random()*9+1)*10000000));
            item.setDate(System.currentTimeMillis());
        });
        return dao.insertList(list);
    }

    /**
     * 扫描该IP
     * @param addr
     * @return
     */
    public List<Port> sacn(String addr) {
        // 获取常用端口
        List<Port> list=portDao.queryList(0,100,new Port());
        // 创建开放端口的容器
        List<Port> result = new ArrayList<>();
        list.forEach(item->{
            // 将Socket交给try管理
            try (Socket socket = new Socket()) {
                // Socket连接指定的主机和端口 等待1秒 如无法连接则该端口未开放
                socket.connect(new InetSocketAddress(addr, item.getPort()), 1000);
                result.add(item);
            } catch (Exception e) {
                System.out.println(addr+":"+item.getPort()+"端口未开放");
            }
        });
        return result;
    }

    /**
     * 扫描主机和端口
     * @param addr
     * @param port
     * @return
     */
    public Port sacn(String addr,Integer port){
        if(this.portList==null){
            this.portList = new HashMap<>();
        }
        try (Socket socket = new Socket()) {
            // 连接该地址端口
            socket.connect(new InetSocketAddress(addr, port), 1000);
            // 如果该端口开着则频率+1
            int number=this.portList.get(port)==null?1:this.portList.get(port)+1;
            this.portList.put(port,number);
            Port port1 = new Port();
            port1.setPort(port);
            port1.setBug("-");
            // 如果频率超过3
            if(number>3){
                portService.set(port1);
            }

            return port1;
        } catch (Exception e) {
            return null;
        }
    }

    public Sacn getInfo(long id) {
        Sacn info=dao.queryInfo(id);
        List<Info> list=infoService.getListBySacn(id);
        info.setInfoList(list);
        return info;
    }
}